//
//  TF_LivingProgress.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2022/2/19.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

public class TF_LivingProgress: UIButton {
    
    ///0-1;
    @objc public var progress:CGFloat = 0
    //完整的颜色
    var wholeColor =  UIColor.init(red: 220/255.0, green: 238/255.0, blue: 255/255.0, alpha: 1.0)
    //进度的颜色
    var speedColor =    UIColor.init(red: 65/255.0, green: 150/255.0, blue: 235/255.0, alpha: 1.0)
    
    @objc func progressUI(value:CGFloat)
    {
        self.progress = value
        
        self.setNeedsDisplay()
    }
  

    public override func draw(_ rect: CGRect) {
        
        let LineWidth:CGFloat = 4

        //获取上下文
         let ctx = UIGraphicsGetCurrentContext()

        let center = CGPoint(x: self.frame.size.width/2, y: self.frame.size.width/2)  //设置圆心位置
        //设置半径
        let radius = self.frame.size.width/2 - (LineWidth/2)
        
        //圆起点位置
        let startA = -Double.pi / 2;
        ////圆终点位置 self.proportion
        let endA   = -(Double.pi / 2) + Double.pi * 2 * 1;

     
        let  path = UIBezierPath.init(arcCenter: center, radius: radius, startAngle: startA, endAngle: endA, clockwise: true)

        //设置线条宽度
        ctx?.setLineWidth(LineWidth)
    
        wholeColor.setStroke()
        ////设置描边颜色
        ctx?.setAlpha(1)
        //把路径添加到上下文
        ctx?.addPath(path.cgPath)
    
        //渲染
        ctx?.strokePath()

        //设置圆心位置
        let center1 = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height/2)
                
        //设置半径
        let radius1 = self.frame.size.width/2 - (LineWidth/2)
        //圆起点位置
        let startA1 = -Double.pi / 2;
        ////圆终点位置
        let endA1 = -Double.pi / 2 + Double.pi * 2 * self.progress;

        let path1 = UIBezierPath.init(arcCenter: center1, radius: radius1, startAngle: startA1, endAngle: endA1, clockwise: true)
        ////设置线条宽度
        ctx?.setLineWidth(LineWidth)
        //设置描边颜色
        speedColor.setStroke()
       

        //把路径添加到上下文
        ctx?.addPath(path1.cgPath)
    
        //渲染
        ctx?.strokePath()
    }
  
}
